local love = require 'love'

local drawg = love.graphics.draw
local setColor = love.graphics.setColor
local stencil = love.graphics.stencil
local setStencilTest = love.graphics.setStencilTest
local drawRectangle = love.graphics.rectangle

local width = Width
local height = Height

local begin = {
    image = love.graphics.newImage("assets/begin.jpg"),
    scale = 0,
}

local credits = {
    image = love.graphics.newImage("assets/credits.jpg"),
    scale = 0,
}

local desk = {
    image = love.graphics.newImage("assets/desk.jpg"),
    y = 0,
    height = 0,
    scale = 0,
    rectWidth = 0,
    rectHeight = 0,
    rx = 0,
    ry = 0,
    color = {0.0745, 0.0627, 0.0667, 1}
}

local rat = {
    image = love.graphics.newImage("assets/rat.jpg"),
    scaleX = 0,
    scaleY = 0,
}

local boar = {
    image = love.graphics.newImage("assets/boar.jpg"),
    scaleX = 0,
    scaleY = 0,
}

local crocodile = {
    image = love.graphics.newImage("assets/crocodile.jpg"),
    scaleX = 0,
    scaleY = 0,
}

local lizard = {
    image = love.graphics.newImage("assets/lizard.jpg"),
    scaleX = 0,
    scaleY = 0,
}

local frog = {
    image = love.graphics.newImage("assets/frog.jpg"),
    scaleX = 0,
    scaleY = 0,
}

local serpent = {
    image = love.graphics.newImage("assets/serpent.jpg"),
    scaleX = 0,
    scaleY = 0,
}

local hydra = {
    image = love.graphics.newImage("assets/hydra.jpg"),
    scaleX = 0,
    scaleY = 0,
}

local function init()
    begin.scale = width / begin.image:getWidth()
    credits.scale = width / credits.image:getWidth()

    desk.height = height * (7/20)
    desk.scaleX = width / desk.image:getWidth()
    desk.scaleY = desk.height / desk.image:getHeight()
    desk.y = height - desk.height

    desk.rectWidth = desk.image:getWidth() * desk.scaleX
    desk.rectHeight = desk.image:getHeight() * desk.scaleY
    desk.rx = 160
    desk.ry = 110

    rat.scaleX = width / rat.image:getWidth()
    rat.scaleY = width * 1.5 / rat.image:getHeight()

    boar.scaleX = width / boar.image:getWidth()
    boar.scaleY = width * 1.5 / boar.image:getHeight()

    crocodile.scaleX = width / crocodile.image:getWidth()
    crocodile.scaleY = width * 1.5 / crocodile.image:getHeight()

    lizard.scaleX = width / lizard.image:getWidth()
    lizard.scaleY = width * 1.5 / lizard.image:getHeight()

    frog.scaleX = width / frog.image:getWidth()
    frog.scaleY = width * 1.5 / frog.image:getHeight()

    serpent.scaleX = width / serpent.image:getWidth()
    serpent.scaleY = width * 1.5 / serpent.image:getHeight()

    hydra.scaleX = width / hydra.image:getWidth()
    hydra.scaleY = width * 1.5 / hydra.image:getHeight()
end

local function drawStart()
    setColor(1, 1, 1)
    drawg(begin.image, 0, 0, 0, begin.scale, begin.scale)
end

local function drawCredits()
    setColor(1, 1, 1)
    drawg(credits.image, 0, 0, 0, credits.scale, credits.scale)
end

local function drawDesk()
    setColor(desk.color)
    drawRectangle("fill", 0, desk.y, desk.rectWidth, desk.rectHeight)
    setColor(1, 1, 1)
    -- 1. 设置模板：绘制一个圆角矩形
    stencil(function()
        -- 绘制带圆角的矩形作为模板
        drawRectangle("fill", 0, desk.y, desk.rectWidth, desk.rectHeight, desk.rx, desk.ry)
    end, "replace", 1)

    -- 2. 只在模板区域内绘制
    setStencilTest("greater", 0)

    -- 3. 绘制图像（会被裁剪成圆角）
    drawg(desk.image, 0, desk.y, 0, desk.scaleX, desk.scaleY)

    -- 4. 关闭模板测试
    setStencilTest()
end

local function drawMonster(match)
    setColor(1, 1, 1)
    if match == 1 then
        drawg(rat.image, 0, 0, 0, rat.scaleX, rat.scaleY)
    elseif match == 2 then
        drawg(boar.image, 0, 0, 0, boar.scaleX, boar.scaleY)
    elseif match == 3 then
        drawg(crocodile.image, 0, 0, 0, crocodile.scaleX, crocodile.scaleY)
    elseif match == 4 then
        drawg(lizard.image, 0, 0, 0, lizard.scaleX, lizard.scaleY)
    elseif match == 5 then
        drawg(frog.image, 0, 0, 0, frog.scaleX, frog.scaleY)
    elseif match == 6 then
        drawg(serpent.image, 0, 0, 0, serpent.scaleX, serpent.scaleY)
    elseif match == 7 then
        drawg(hydra.image, 0, 0, 0, hydra.scaleX, hydra.scaleY)
    end
end

return{
    init = init,
    drawStart = drawStart,
    drawCredits = drawCredits,
    drawDesk = drawDesk,
    drawMonster = drawMonster
}